package com.xdhl.fu.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.constant.SecurityConstants;
import com.xdhl.fu.common.core.web.controller.BaseController;
import com.xdhl.fu.common.core.web.domain.AjaxResult;
import com.xdhl.fu.common.security.annotation.RequiresPermissions;
import com.xdhl.fu.common.security.utils.SecurityUtils;
import com.xdhl.fu.member.api.RemoteMemberBasicService;
import com.xdhl.fu.member.domain.MemberAddress;
import com.xdhl.fu.member.service.ICityService;
import com.xdhl.fu.member.service.IDistrictService;
import com.xdhl.fu.member.service.IMemberAddressService;
import com.xdhl.fu.member.service.IProvinceService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 会员地址控制层
 *
 * @author 朱力维
 * @time 2022/2/27 16:38
 */
@RestController
@RequestMapping("/address")
public class MemberAddressController extends BaseController {
    @Resource
    private IMemberAddressService memberAddressService;

    @Resource
    private RemoteMemberBasicService remoteMemberBasicService;

    @Resource
    private IProvinceService provinceService;

    @Resource
    private ICityService cityService;

    @Resource
    private IDistrictService districtService;

    /**
     * 获取会员地址详细信息
     */
    @RequiresPermissions("member:address:query")
    @GetMapping(value = "/{mDataId}")
    public AjaxResult getInfo(@PathVariable("mDataId") String mDataId) {
        LambdaQueryWrapper<MemberAddress> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberAddress::getMemberDataId, mDataId);
        return AjaxResult.success(memberAddressService.list(lqw));
    }

    /**
     * 获取登录会员地址列表
     */
    @GetMapping("/list")
    public AjaxResult getMemberAddress(HttpServletRequest request) {
        return AjaxResult.success(memberAddressService.getAddressInfo(request));
    }

    /**
     * 新增地址信息
     */
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody MemberAddress address) {
        address.setCreateBy(SecurityUtils.getUsername());
        address.setCreateTime(LocalDateTime.now());
        address.setUpdateBy(address.getCreateBy());
        address.setUpdateTime(address.getCreateTime());
        address.setProvince(provinceService.getById(Integer.parseInt(address.getProvince())).getProvinceName());
        address.setCity(cityService.getById(Integer.parseInt(address.getCity())).getCityName());
        address.setArea(districtService.getById(Integer.parseInt(address.getArea())).getDistrictName());
        address.setMemberDataId(remoteMemberBasicService.getDataIdSelf(SecurityUtils.getToken(), SecurityConstants.INNER).getData());
        if (memberAddressService.save(address))
            return AjaxResult.success("添加成功");
        return AjaxResult.error("添加失败，请稍后重试");
    }

    /**
     * 删除地址信息
     */
    @Transactional
    @DeleteMapping("/{dataId}")
    public AjaxResult remove(@PathVariable String dataId) {
        return toAjax(memberAddressService.removeById(dataId));
    }

    /**
     * 设置默认地址
     */
    @Transactional
    @PutMapping("/setDefault")
    public AjaxResult edit(@RequestBody MemberAddress address) {
        LambdaQueryWrapper<MemberAddress> lqw = new LambdaQueryWrapper();
        lqw.eq(MemberAddress::getMemberDataId, address.getMemberDataId());
        List<MemberAddress> list = memberAddressService.list(lqw);
        list.forEach(item -> {
            if (item.getADefault() == 1) {
                item.setADefault(0);
                memberAddressService.updateById(item);
            }
        });
        address.setADefault(1);
        return toAjax(memberAddressService.updateById(address));
    }
}
